<template>
  <el-form :model="form" :inline="true" ref="addEditForm" :modal-append-to-body="false">
    <el-dialog
      :title="`${typeText + title}`"
      :before-close="dialogClose"
      :modal-append-to-body="false"
      :visible.sync="dialogVisible"
      :close-on-click-modal="closeOnClickModal"
      :width="dialogWidth"
    >
      <!-- 表单主体 -->
      <slot></slot>
      <fml-input
        :FormConfig="formConfig"
        :showMore="true"
        ref="fmlInput"
        @getValidate="validateResult"
      ></fml-input>
      <slot name="other"></slot>
      <!-- 底部按钮区 -->
      <!-- dialogType 0为新增修改，默认项  1查看  2审核 -->
      <!-- footer 可为查看--只有取消按钮，审核--取消，通过，不通过按钮，新增修改--保存，取消按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose" size="small" class=" cancel ">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm(3)"
          size="small"
          class="margin-l30"
          :loading="loading"
          v-if="otherPublic&&publicStatus==0"
        >{{publicBtn}}</el-button>
        <el-button
          type="primary"
          @click="submitForm()"
          size="small"
          class="margin-l30"
          :loading="loading"
          v-if="dialogType!='audit'&&dialogType!='look'"
        >{{dialogType=='newEdit'?'重新提交':saveBtn}}</el-button>
        <el-button
          type="primary"
          @click="submitForm(1)"
          data-audit="1"
          size="small"
          :loading="yesloading"
          v-if="dialogType=='audit'"
        >通 过</el-button>
        <el-button
          type="primary"
          @click="submitForm(2)"
          data-audit="2"
          size="small"
          :loading="noloading"
          v-if="dialogType=='audit'"
        >不通过</el-button>
      </span>
    </el-dialog>
  </el-form>
</template>

<script>
import { mapState } from "vuex";
import { Validator } from "vee-validate";
import { listService,globalService } from "@/fmlService";

export default {
  name: "fmlEditDialog",
  data() {
    return {
      loading:false,
      yesloading: false,
      noloading: false,
      audit: "" //如果是审核的话，通过此参数1 通过 2不通过
    };
  },
  props: {
    otherPublic:{
      type:Boolean,
      default:false
    },
    saveBtn:{
      type:String,
      default:'保 存'
    },
    publicBtn:{
      type:String,
      default:'发 布'
    },
    formConfig: {}, //表单配置
    title: String, //弹窗标题
    form: Object,
    addParam: Object, //新增参数
    auditParam:Object,
    addApi: Function, //新增接口
    publicApi: Function, //新增接口
    editParam: Object, //编辑参数
    editApi: Function, //编辑接口
    auditApi: Function, //审核接口
    publicStatus:Number,//是否已发布
    sendparam: "", //审核回传的key
    addDuplicateText: String, // 添加时已存在错误提示信息
    dialogWidth: {
      //弹窗宽
      type: String,
      default: "80%"
    },
    closeOnClickModal: {
      // 是否可以通过点击 modal 关闭 Dialog
      default: false,
      type: Boolean
    }
  },
  computed: {
    ...mapState(["dialogVisible", "editData", "imgUpload", "dialogType"]),
    typeText() {
      return this.dialogType=='newEdit'?'重新提交': this.editData ? (this.dialogType == "audit"||this.dialogType=='auditEdit')? "审核": this.dialogType=='look'?'查看':"修改": "新增";
    }
  },
  watch:{
    dialogVisible(){
      if(this.dialogVisible&&(this.dialogType=='audit'||this.dialogType=='look')){
        this.formConfig.defaultConfig.form.map(item=>{
          item.tdData.map(item1=>{
            if(item1.notDisabled)return
            item1.disabled=true
          })
        })
      }
    }
  },
  methods: {
    // 关闭弹窗
    dialogClose() {
      this.formConfig.defaultConfig.form.map(item=>{
        item.tdData.map(item1=>{
          if(item1.notDisabled)return
          item1.disabled=false
        })
      })
      this.$refs.fmlInput.clearValidate(); //每次关闭弹窗清空校验
      this.$store.commit("setDialogVisible", false);
    },
    // 新增修改
    async submitForm(audit) {
      this.audit = audit || "";
      this.$refs.fmlInput.callMethod();
     
    },

    // 新添加解决校验不同步问题
    async validateResult(result) {
      await this.addOrEditApi(result);
    },
    // 接口公用
    async commonFun(){
       let auditParam = this.auditParam;
      if (this.dialogType== 'audit') {
        if (!this.sendparam)return this.$message({message:"审核的key不存在",type:'error',showClose:true});
        auditParam[this.sendparam] = this.audit;
      }
      
      const Api =this.otherPublic&&this.audit==3&&this.dialogType!='look'?this.publicApi:(this.editData? (this.dialogType == "audit"? this.auditApi: this.editApi):this.addApi); //新增还是修改接口
      let Param = this.editData? this.dialogType == "auditEdit"? { ...this.editParam, ...auditParam }:(this.dialogType=='audit'?auditParam : this.editParam) : this.addParam; //新增还是修改的还是审核参数
      if (this.aduit == 1) {
        this.yesloading = true;
      } else if (this.aduit == 2) {
        this.noloading = true;
      }
      this.loading = true;
      // 发布查看
      if(this.audit==3){
        Param={...Param,...this.editParam}
      }
      const [err] = await Api(Param);
      if (err) {
        this.loading = false;
        this.yesloading = false;
        this.noloading = false;

        return this.$message({message:err.message || `${this.typeText=='查看'||this.audit==3?'发布':this.typeText}失败`,type:'error',showClose:true});
      }
      this.$message({message:`${this.typeText=='查看'||this.audit==3?'发布':this.typeText}成功`,type:'success',showClose:true});
      this.dialogClose();
      this.loading = false;
      this.yesloading = false;
      this.noloading = false;
      this.$store.commit("updateTable");
    },
    async addOrEditApi(result) {
      if (!result) return;
       this.commonFun()
      // this.$refs.fmlInput.uploadAutoFile(async (res)=>{
      //   console.log(res)
      //   this.commonFun({fileIds:res})
      // })
     
    }
  },
  created() {
    this.$store.commit("setDialogVisible", false);
  }
};
</script>

